package lovely.gwt.jsonrpc.rebind.post;

import rocket.generator.rebind.codeblock.TemplatedFileCodeBlock;
import rocket.generator.rebind.type.Type;
import rocket.util.client.Checker;

public class ReadObjectTemplatedFile extends TemplatedFileCodeBlock {

	/**
	 * The serializer type for the model
	 */
	private Type serializerType;

	protected Type getSerializerType() {
		Checker.notNull("field:serializerType", serializerType);
		return this.serializerType;
	}

	public void setSerializerType(final Type serializerType) {
		Checker.notNull("parameter:serializerType", serializerType);
		this.serializerType = serializerType;
	}

	/**
	 * The return type of the service method
	 */
	private Type returnType;

	protected Type getReturnType() {
		Checker.notNull("returnType:returnType", returnType);
		return this.returnType;
	}

	public void setReturnType(final Type returnType) {
		Checker.notNull("parameter:returnType", returnType);
		this.returnType = returnType;
	}

	/**
	 * The key of the template file
	 */
	private String template;

	public void setTemplate(final String template) {
		Checker.notNull("parameter:template", template);
		//TODO write test
		if (!ReadObjectConstants.TEMPLATE_NAMES.containsKey(template)) {
			Checker.fail("parameter:template", "unknown key");
		}
		this.template = template;
	}

	@Override
	protected String getResourceName() {
		Checker.notNull("field:template", template);
		return ReadObjectConstants.TEMPLATE_NAMES.get(template);
	}

	@Override
	protected Object getValue0(String name) {
		Object value = null;
		while (true) {
			if (ReadObjectConstants.RETURN_TYPE.equals(name)) {
				value = this.getReturnType();
				break;
			}
			if (ReadObjectConstants.SERIALIZER_TYPE.equals(name)) {
				value = this.getSerializerType();
				break;
			}
			break;
		}
		return value;
	}

}
